﻿using Chat_IService;
using Chat_Model;
using Chat_Model.ViewModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Memory;
using System;

namespace Chat_WebApi.Controllers.v1
{
    /// <summary>
    /// 消息
    /// </summary>
    [Route("/api/v1/[controller]")]
    [ApiController]
    [Authorize]
    public class SignalRController : ControllerBase
    {
        private readonly IHubContext<SignalRHub> _hubContext;
        private readonly IMemoryCache _cache;
        private readonly ILarUsersService _larUsersService;
        public SignalRController(IHubContext<SignalRHub> hubClients,
            IMemoryCache memoryCache,
            ILarUsersService larUsersService)
        {
            this._hubContext = hubClients;
            this._cache = memoryCache;
            this._larUsersService = larUsersService;
        }
        /// <summary>
        /// 测试
        /// </summary>
        /// <returns></returns>
        [HttpPost("index")]
        public string Index()
        {
            return "HELLO World";
        }
        /// <summary>
        /// 广播
        /// </summary>
        [HttpPost("SendToAll")]
        public void SendToAll()
        {
            // 给所有人推送消息
            this._hubContext.Clients.All.SendAsync("toall", "后端", "你好", "给所有人发");
        }
        /// <summary>
        /// 连接
        /// </summary>
        [HttpPost("Connected")]
        public MessageModel<object> Connected([FromBody] SignalrConnectedParam param)
        {
            int uid = this._larUsersService.GetUserId();
            this._cache.Set("re_" + uid, param.ConnectionId, new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(10)));
            return new MessageModel<object>()
            {
                Code = 200,
            };
        }
        ///// <summary>
        ///// 单播
        ///// </summary>
        ///// <param name="user"></param>
        //[HttpPost("SendToClient")]
        //public void SendToClient([FromBody] string user)
        //{
        //    _cache.Get()

        //    //给指定人推送消息
        //    _hubContext.Clients.Client(user).SendAsync("toall", "后端", $"你好{user}", "只给你发");
        //}
        /// <summary>
        /// 群发单播
        /// </summary>
        /// <param name="user"></param>
        [HttpPost("SendToClients")]
        public void SendToClients(string user)
        {
            //给指定人推送消息
            this._hubContext.Clients.Clients(user).SendAsync("toall", "后端", $"你好{user}", "只给你发");
        }
        /// <summary>
        /// 组播
        /// </summary>
        /// <param name="user"></param>
        [HttpPost("SendToGroup")]
        public void SendToGroup(string user)
        {
            //给指定人推送消息
            this._hubContext.Clients.Group(user).SendAsync("toall", "后端", $"你好{user}", "只给你发");
        }
        /// <summary>
        /// 群发组播
        /// </summary>
        /// <param name="user"></param>
        [HttpPost("SendToGroups")]
        public void SendToGroups(string user)
        {
            //给指定人推送消息
            this._hubContext.Clients.Groups(user).SendAsync("toall", "后端", $"你好{user}", "只给你发");
        }
        #region 组管理
        /// <summary>
        /// 添加组成员
        /// </summary>
        [HttpPost("AddToGroup")]
        public void AddToGroup([FromBody] GroupParam param)
        {
            //给指定人推送消息
            this._hubContext.Groups.AddToGroupAsync(param.ConnectionId, param.GroupName);
        }
        /// <summary>
        /// 移除组成员
        /// </summary>
        /// <param name="user"></param>
        [HttpPost("RemoveFromGroup")]
        public void RemoveFromGroup([FromBody] GroupParam param)
        {
            //给指定人推送消息
            this._hubContext.Groups.RemoveFromGroupAsync(param.ConnectionId, param.GroupName);
        }
        #endregion
    }
}
